#
# Cookbook Name:: wsmq
# Recipe:: install
# This recipe will install the MQ Server
# Copyright 2013, IBM Corporation
#
# All rights reserved - Do Not Redistribute
#

#include_recipe "gitclientsetup"
#deployment = node[:deployment]
cuser = "root"
cgroup = "root"
version = node[:mq_server][:version]
arch = node[:mq_server][:arch]
gskver = node[:mq_server][:gskver]
tmpdir = node[:mq][:tmpdir]  

#startinstr :mq_server

# Create mqm user and group
#group "#{node[:mq_server][:group]}" do
#  members ["root","#{cuser}"]
#  action :create
#end



bash "Setup channels" do
    user "root"
    cwd "/opt/mqm"
    creates "/tmp/channels-setup.done"
    code <<-EOH
      #before starting the qmgr,  let's set the MaxChannels and stuff...
      echo "CHANNELS:" >> /var/mqm/qmgrs/#{node[:mq][:mqmgr]}/qm.ini
      echo "   MaxChannels=1000" >> /var/mqm/qmgrs/#{node[:mq][:mqmgr]}/qm.ini
      echo "   MaxActiveChannels=1000" >> /var/mqm/qmgrs/#{node[:mq][:mqmgr]}/qm.ini
      echo "   ClientIdle=300 " >> /var/mqm/qmgrs/#{node[:mq][:mqmgr]}/qm.ini
      touch "/tmp/channels-setup.done"
    EOH
end

`(#{node[:mq][:installDir]}/bin/dspmq | grep #{node[:mq][:mqmgr]}| grep Running)`
if $? != 0
    bash "Start Queue Manager" do
      user "root"
      cwd "/opt/mqm"
      code <<-EOH
        su mqm -c "#{node[:mq][:installDir]}/bin/strmqm #{node[:mq][:mqmgr]}"      
      EOH
    end
end

template "/tmp/createqueues.txt" do
      source "createqueues.txt.erb"
      owner "mqm"
      action :create
end

bash "Create Default Queues" do
    user "root"   
    cwd "/opt/mqm/"
    code <<-EOH
      su mqm -c "#{node[:mq][:installDir]}/bin/runmqsc #{node[:mq][:mqmgr]} < /tmp/createqueues.txt" > /tmp/queuecreation.log 2>&1
    EOH
end

#cookbook_file "/tmp/jmsadminredirect.sh" do
#    source "jmsadminredirect.sh"
#    user "mqm"
#    owner "mqm"
#    action :create
#end

#TODO:  Condition seems unreliable....
# not checking might cost time,  but doesn't hurt anything i think.
`ps -efla |grep runmqlsr |grep #{node[:mq][:mqmgr]}`
#if $? != 0
    bash "Start MQ Listener" do
      user "mqm"
      group "mqm"
      cwd "/opt/mqm/"
      code <<-EOH
        nohup /opt/mqm/bin/runmqlsr  -m #{node[:mq][:mqmgr]} -t TCP -p #{node[:mq][:mqmgrport]} &
      EOH
    end
#end
  
bash "Open MQ Listener port" do
    user "root"
    code <<-EOH
      /sbin/iptables -A INPUT -p tcp --dport #{node[:mq][:mqmgrport]} -j ACCEPT
      service iptables save
    EOH
end



